﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using DevM.Authorization;
using DevM.Controllers;
using DevM.Users;
using DevM.Web.Models.Users;
using DevM.Users.Dto;
using DevM.Development.Projects;
using DevM.Web.Models.Projects;
using DevM.Development.Projects.Dto;

namespace DevM.Web.Controllers
{
    [AbpMvcAuthorize(PermissionNames.Pages_Projects)]
    public class ProjectsController : DevMControllerBase
    {
        private readonly IProjectAppService projectAppService;
        private readonly IUserAppService userAppService;

        public ProjectsController(IProjectAppService projectAppService, IUserAppService userAppService)
        {
            this.projectAppService = projectAppService;
            this.userAppService = userAppService;
        }

        public async Task<ActionResult> Index(GetProjectListInput input)
        {
            var roles =await projectAppService.GetAllRoles();
            var projects = await projectAppService.GetProjectList(input);
            var model = new ProjectListViewModel
            {
                Projects = projects,
                Roles = roles
            };
            return View(model);
        }

        public async Task<ActionResult> EditProjectModal(long projectId)
        {
            var project = await projectAppService.GetProject(new EntityDto<long>(projectId));
            var roles = await projectAppService.GetAllRoles();
            var model = new EditProjectModalViewModel
            {
                Project = project,
                Roles = roles
            };
            return View("_EditProjectModal", model);
        }

        public async Task<ActionResult> Environments(long projectId)
        {
            var environments = await projectAppService.GetEnvironmentList(new EntityDto<long>(projectId));
            var model = new EnvironmentListViewModel
            {
                Project = await projectAppService.GetProject(new EntityDto<long>(projectId)),
                Environments = environments
            };
            ViewBag.ProjectId = projectId;
            return View(model);
        }

        public async Task<ActionResult> EditEnvironmentModal(long environmentId)
        {
            var environment = await projectAppService.GetEnvironment(new EntityDto<long>(environmentId));
            var model = new EditEnvironmentViewModel
            {
                Environment = environment
            };
            return View("_EditEnvironmentModal", model);
        }
    }
}
